<template>
  <div class="registration-order-detail">
    <!-- 页面头部 -->
    <div class="page-header">
      <h2>挂号订单详情-{{ orderDetail?.orderInfo?.statusText }}</h2>
      <div class="header-actions">
        <button @click="goBack" class="action-button back">
          返回
        </button>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <p>加载中...</p>
    </div>

    <!-- 订单详情 -->
    <div v-else-if="orderDetail" class="detail-content">
      <!-- 订单信息 -->
      <div class="info-section">
        <h3>订单信息</h3>
        <div class="info-grid">
          <div class="info-item">
            <label>订单编号：</label>
            <span>{{ orderDetail.orderInfo?.orderNo }}</span>
          </div>
          <div class="info-item">
            <label>挂号费：</label>
            <span>¥{{ orderDetail.orderInfo?.fee }}</span>
          </div>
          <div class="info-item">
            <label>状态：</label>
            <span :class="['status-badge', getStatusClass(orderDetail.orderInfo?.status)]">
              {{ orderDetail.orderInfo?.statusText }}
            </span>
          </div>
          <div class="info-item">
            <label>提交时间：</label>
            <span>{{ formatDateTime(orderDetail.orderInfo?.createdAt) }}</span>
          </div>
        </div>
      </div>

      <!-- 患者信息 -->
      <div class="info-section">
        <h3>患者信息</h3>
        <div class="info-grid">
          <div class="info-item">
            <label>用户名：</label>
            <span>{{ orderDetail.orderInfo?.username || '未知' }}</span>
          </div>
          <div class="info-item">
            <label>联系电话：</label>
            <span>{{ orderDetail.orderInfo?.userPhone || '未知' }}</span>
          </div>
        </div>
      </div>

      <!-- 医生信息 -->
      <div class="info-section">
        <h3>医生信息</h3>
        <div class="info-grid">
          <div class="info-item">
            <label>医生姓名：</label>
            <span>{{ orderDetail.orderInfo?.doctorName || '未知' }}</span>
          </div>
          <div class="info-item">
            <label>科室：</label>
            <span>{{ orderDetail.orderInfo?.departmentName || '未知' }}</span>
          </div>
          <div class="info-item">
            <label>医院：</label>
            <span>{{ orderDetail.orderInfo?.hospitalName || '未知' }}</span>
          </div>
          <div class="info-item">
            <label>预约就诊时间：</label>
            <span>{{ formatDateTime(orderDetail.orderInfo?.scheduleTime) }}</span>
          </div>
        </div>
      </div>

      <!-- 支付信息 -->
      <div v-if="orderDetail.orderInfo?.status >= 1" class="info-section">
        <h3>支付信息</h3>
        <div class="info-grid">
          <div class="info-item">
            <label>支付方式：</label>
            <span>{{ getPaymentMethodText(orderDetail.orderInfo?.paymentMethod) }}</span>
          </div>
          <div class="info-item">
            <label>支付时间：</label>
            <span>{{ formatDateTime(orderDetail.orderInfo?.paymentTime) }}</span>
          </div>
          <div class="info-item">
            <label>优惠金额：</label>
            <span>¥{{ orderDetail.orderInfo?.discountAmount || 0 }}</span>
          </div>
          <div class="info-item">
            <label>实际支付：</label>
            <span>¥{{ orderDetail.orderInfo?.actualPayment || 0 }}</span>
          </div>
        </div>
      </div>

      <!-- 退款信息 -->
      <div v-if="orderDetail.orderInfo?.status === 3 || orderDetail.orderInfo?.status === 4" class="info-section">
        <h3>退款信息</h3>
        <div class="info-grid">
          <div class="info-item">
            <label>退款金额：</label>
            <span>¥{{ orderDetail.orderInfo?.refundAmount || 0 }}</span>
          </div>
          <div class="info-item">
            <label>退款时间：</label>
            <span>{{ formatDateTime(orderDetail.orderInfo?.refundTime) }}</span>
          </div>
        </div>
      </div>

      <!-- 取消信息 -->
      <div v-if="orderDetail.orderInfo?.status === 2" class="info-section">
        <h3>取消信息</h3>
        <div class="info-grid">
          <div class="info-item">
            <label>取消原因：</label>
            <span>{{ orderDetail.orderInfo?.cancelReason || '未知' }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 错误状态 -->
    <div v-else-if="error" class="error-container">
      <p class="error-message">{{ error }}</p>
      <button @click="loadOrderDetail" class="retry-button">重试</button>
    </div>
    
    <!-- 空状态 -->
    <div v-else class="error-container">
      <p>订单不存在或加载失败</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import api from '../utils/request';

const router = useRouter();
const route = useRoute();

// 响应式数据
const loading = ref(false);
const error = ref<string | null>(null);
const orderDetail = ref<any>(null);

// 获取订单详情
const loadOrderDetail = async () => {
  try {
    loading.value = true;
    const orderId = Number(route.params.id);
    
    if (!orderId || orderId <= 0) {
      throw new Error('订单ID无效');
    }
    
    const response = await api.registrationOrder.getDetail(orderId);
    
    if (response && response.data) {
      orderDetail.value = response.data;
      error.value = null; // 清除错误状态
    } else {
      throw new Error('订单不存在');
    }
  } catch (err) {
    console.error('获取挂号订单详情失败:', err);
    const errorMessage = err instanceof Error ? err.message : '获取挂号订单详情失败，请重试';
    error.value = errorMessage;
  } finally {
    loading.value = false;
  }
};

// 获取状态样式类
const getStatusClass = (status: number) => {
  switch (status) {
    case 0: return 'status-pending';
    case 1: return 'status-completed';
    case 2: return 'status-cancelled';
    case 3: return 'status-refunded';
    case 4: return 'status-refunded'; // 兼容状态4
    default: return 'status-unknown';
  }
};

// 获取支付方式文本
const getPaymentMethodText = (method: string) => {
  const methodTexts = {
    'wechat': '微信支付',
    'alipay': '支付宝',
    'bank_card': '银行卡'
  };
  return methodTexts[method as keyof typeof methodTexts] || method || '未知';
};

// 格式化日期时间
const formatDateTime = (dateTime: string) => {
  if (!dateTime) return '-';
  return new Date(dateTime).toLocaleString('zh-CN');
};

// 返回上一页
const goBack = () => {
  router.back();
};

// 生命周期钩子
onMounted(() => {
  loadOrderDetail();
});
</script>

<style scoped>
.registration-order-detail {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.page-header h2 {
  margin: 0;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.action-button {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.action-button.back {
  background-color: #f5f5f5;
  color: #666;
  border: 1px solid #ddd;
}

.action-button.back:hover {
  background-color: #e6e6e6;
  border-color: #ccc;
}

/* 加载状态 */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #409eff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 10px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 详情内容 */
.detail-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.info-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.info-section h3 {
  margin: 0 0 15px 0;
  color: #333;
  font-size: 18px;
  font-weight: 600;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 15px;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.info-item label {
  font-weight: 500;
  color: #666;
  min-width: 100px;
}

.info-item span {
  color: #333;
}

/* 状态标签 */
.status-badge {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.status-pending {
  background-color: #fff7e6;
  color: #fa8c16;
}

.status-completed {
  background-color: #f6ffed;
  color: #52c41a;
}

.status-cancelled {
  background-color: #fff2f0;
  color: #ff4d4f;
}

.status-refunded {
  background-color: #f0f5ff;
  color: #1890ff;
}

.status-unknown {
  background-color: #f5f5f5;
  color: #999;
}

/* 错误状态 */
.error-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 60px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.error-message {
  color: #f56c6c;
  margin-bottom: 15px;
  font-size: 14px;
}

.retry-button {
  padding: 8px 20px;
  background-color: #f56c6c;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

.retry-button:hover {
  background-color: #e74c3c;
}

.error-container p {
  color: #666;
  font-size: 16px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .registration-order-detail {
    padding: 10px;
  }
  
  .page-header {
    flex-direction: column;
    gap: 15px;
    align-items: stretch;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .info-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 5px;
  }
  
  .info-item label {
    min-width: auto;
  }
}
</style> 